<?php 
require_once '../../conf/config.php.inc';
require_once(SYBMAS_LIB_PATH.'/liblogin.php.inc');
//require_once(SYBMAS_LAYOUT_PATH.'/layout.php.inc');
require_once(SYBMAS_LIB_PATH.'/libsybmas.php.inc');
require_once(SYBMAS_LIB_PATH.'/fpdf.php.inc');
require_once(SYBMAS_LAYOUT_PATH.'/menu.php.inc');
verificar_inicio_sesion();
date_default_timezone_set ("America/Mexico_City");

$dbname=$_SESSION['sybmas.user.dbname'];
$login=obtener_nombre_usuario();
foreach($_POST as $key => $value){
	$$key=$value;
}

$query="SELECT db.name,o.name,grantor.name,grantee.name,tp.nombre,ap.accion ,po.fecha FROM ".
		SYBMAS_DATABASE_NAME. "..sybmas_permisoObjeto po, ".
		SYBMAS_DATABASE_NAME. "..sybmas_tipoPermiso tp, ".
		SYBMAS_DATABASE_NAME. "..sybmas_accionPermiso ap, ".
		"master..sysdatabases db, ".
		"$dbname..sysobjects o, ".
		"$dbname..sysusers grantee, ".
		"$dbname..sysusers grantor ".
		"WHERE db.name='$dbname' ".
		"AND po.id_tipoPermiso=tp.id_tipoPermiso ".
		"AND po.id_accion=ap.id_accion ".
		"AND po.suid_otorga=grantor.suid ".
		"AND po.suid_recibe=grantee.suid ".
		"AND po.tableid=o.id ".
		"AND po.dbid=db.dbid ".
		"ORDER BY po.fecha DESC";

class PDF extends FPDF{
	//Cabecera de página
function Header(){
	global $dbname;
	global $login;
	//Logo
    $this->Image(SYBMAS_IMG_PATH.'/sybmas_logo.jpg',10,8,33);
    //Arial bold 15
    $this->SetFont('Arial','B',15);
    //Movernos a la derecha
    $this->Cell(60);
    //Título
    $this->Cell(70,10,'Privilegios Otorgados/Revocados',0,0,'C');
    $this->Ln(15);
    
    $this->SetFont('Arial','',10);
    
    $this->Cell(60,10,"Base de datos: $dbname",0,0,'L');
    
    $this->Cell(50,10,"Login: $login",0,0,'L');
    
    $time=time();
    $ftime=date('H:i:s',$time);
    $fdate=date('j/m/Y',$time);
    
    $this->Cell(50,10,"Fecha: $fdate",0,0,'L');
    $this->Cell(50,10,"Hora: $ftime",0,0,'L');
    //Salto de línea
    $this->Ln(20);
    
    $this->SetFont('Arial','B',10);
	$this->Cell(35,6,'Tabla',1,0,'C');
	$this->Cell(30,6,'Otorga',1,0,'C');
	$this->Cell(30,6,'Recibe',1,0,'C');
	$this->Cell(30,6,'Tipo',1,0,'C');
	$this->Cell(30,6,'Accion',1,0,'C');
	$this->Cell(35,6,'Fecha',1,0,'C');
	$this->Ln();
}

//Pie de página
function Footer(){
    //Posición: a 1,5 cm del final
    $this->SetY(-15);
    //Arial italic 8$login
    $this->SetFont('Arial','I',8);
    //Número de página
    $this->Cell(0,10,'Pagina '.$this->PageNo().'/{nb}',0,0,'C');
}

}
$conn=start_sybmas_connection();
$result=sybase_query($query,$conn);	



//Creación del objeto de la clase heredada
$pdf=new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();

$num_rows=sybase_num_rows($result);
if($num_rows<=0){
	$pdf->SetFont('Arial','B',10);
	$pdf->Cell(190,15,'La consulta no obtubo registros',1,0,'C');
}else{
	$pdf->SetFont('Times','',10);
	while($data=sybase_fetch_row($result)){
		$pdf->Cell(35,6,$data[1],1);
		$pdf->Cell(30,6,$data[2],1);
		$pdf->Cell(30,6,$data[3],1);
		$pdf->Cell(30,6,$data[4],1);
		$pdf->Cell(30,6,$data[5],1);
		$pdf->Cell(35,6,$data[6],1);
		$pdf->Ln();
	}
}
sybase_free_result($result);
sybase_close($conn);
$pdf->Output();
?>